package org.example.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

@Configuration
public class ThymeleafAutoConfig {
    @Bean
    public ITemplateResolver templateResolver() {
        // 模板解析器
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        //模板所在目录，相对于当前classloader的classpath。
        resolver.setPrefix("templates/json/");
        // 模板文件后缀
        resolver.setSuffix(".tpl");
//        if (mode == TemplateMode.HTML) {
//            resolver.setSuffix(".html");
//        } else {
//            resolver.setSuffix(".tpl");
//        }
        // 设置模式
        resolver.setTemplateMode(TemplateMode.TEXT);
        return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
        // 模板引擎
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        return templateEngine;
    }
}
